from import_export.fields import Field
from import_export.resources import ModelResource

from .models import Store, ServeGift


class ServeGiftResource(ModelResource):
    id = Field(attribute='id', column_name="序号")
    store_location = Field(attribute="store_location", column_name="存放位置", readonly=True)
    amount = Field(attribute="amount", column_name="数量/单位", readonly=True)

    class Meta:
        model = ServeGift
        exclude = ('created', 'poster', 'count', 'store', 'unit', 'place')

    @classmethod
    def field_from_django_field(cls, field_name, django_field, readonly):
        field = super().field_from_django_field(field_name, django_field, readonly)
        if django_field.verbose_name:
            field.column_name = django_field.verbose_name
        return field
